<?php

// Prevent individual file access
if(!defined('DIR_BASE')) exit;

/** 
 * The class DefinitionModel contains functions for the table 'words' and 'userwords'.
 */
class DefinitionModel{
	/**
	 * Instance of the table 'words'
	 */
	private $tableWords;
	/** 
	 * Instance of the table 'userwords'
	 */
	private $tableUserwords;
	
	/** 
	 * creating instances of the table 'words' and 'userwords'
	 */
	public function __construct(){
		$this->tableWords = new Table('words', array(
			'id',
			'word',
			'definition'
		));
		$this->tableUserwords = new Table('userwords', array(
			'id',
			'userid',
			'wordid',
			'definition'
		));
	}
	
	/**
	 * Get a list of definitions given by users
	 * @param String $word The given word
	 * @return Array List of definitions by users
	 */
	public function getUserDefinitions($word){
		$qb = new QueryBuilder();
		$qry = $qb	-> select('id')
					-> table('words')
					-> where('word', $word)
					-> build();
		$wordid = Database::query($qry);
		
		$wordlist = array();
		if(empty($wordid)) return $wordlist;
		$qry = $qb	-> select('w.userid', 'w.definition', 'u.username')
					-> table('userwords', 'w')
					-> join('users', 'u', 'u.id', 'w.userid')
					-> where('w.wordid', $wordid)
					-> build();
		$wordlist = Database::queryList($qry);
		
		return $wordlist;
	}
	
	/**
	 * Get the defintion given by a super user or the web
	 * @param String $word The given word
	 * @return String The definition
	 */
	public function getPublicDefinition($word){
		$qb = new QueryBuilder();
		$qry = $qb	-> select('definition')
					-> table('words')
					-> where('word', $word)
					-> build();
		$row = Database::query($qry);
		if(!empty($row))
			return $row;
		else 
			return null;
	}
	
	/**
	 * Get a list of words with the public definition
	 * @return Array A list of words
	 */
	public function getWordList($userid = null){
		$qb = new QueryBuilder();
		$qb	-> select('w.id', 'w.word')
			-> table('words', 'w');
		
		if($userid != null){
			$qb	-> select('u.definition')
				-> join('userwords', 'u', 'u.wordid', 'w.id')
				-> where('u.userid', $userid);
		}else{
			$qb	-> select('w.definition');
		}
		$qry = $qb -> build();
		$list = Database::queryList($qry);
		return $list;
	}
	
	/**
	 * Save a definition given by an user
	 * @param String $word The given word
	 * @param String $definition The given definition
	 */
	public function saveUserDefinition($word, $definition){
		$qb1 = new QueryBuilder();
		$qry1 = $qb1 -> select('id')
					 -> table('words')
					 -> where('word', $word)
					 -> build();
		$wordid = Database::query($qry1);
		
		$qb = new QueryBuilder();
		$qry = $qb 	-> select()
					-> table('words')
					-> where('word', $word)
					-> build();
		$wordRow = Database::query($qry);
		
		if(empty($wordRow)){
			$wordid = $this->tableWords->store( array('word' => $word) );
		}
		$this->tableUserwords->store( array(
			'userid'	 => $_SESSION['userid'],//UserController::getUserId(),
			'wordid' 	 => $wordid,
			'definition' => $definition
		));
	}
	
	/**
	 * Save the public definition
	 */
	public function savePublicDefinition($word, $definition){
		$qb = new QueryBuilder();
		$qry = $qb 	-> select()
					-> table('words')
					-> where('word', $word)
					-> build();
		$wordRow = Database::query($qry);
					
		if(empty($wordRow)){	
		$this->tableWords->store(array('word' => $word, 'definition' => $definition));
		}
	}
}

?>